import requests
import json
import base64
import time
from plugins.config.config_fofa import email,key

def fofa(fofasearch,page,size):
    header = {'user-agent': 'WebFox/0.0.1'}
    fofa_url = "https://fofa.so/api/v1/info/my?email={}&key={}".format(email,key)
    res = requests.get(fofa_url, headers=header)
    if 'errmsg' not in res.text:
        print("[+] Email and Apikey Check Success, Start Search...\n")
        time.sleep(0.75)
        sentence = base64.b64encode(fofasearch.encode('utf-8')).decode("utf-8")
        #print(sentence)
        fofa_search_url = "https://fofa.so/api/v1/search/all?email={}&key={}&qbase64={}&page={}&size={}".format(email,key,sentence,page,size)
        res = requests.get(fofa_search_url, headers=header)
        if 'errmsg' not in res.text:
            result = json.loads(res.text)
            for link in result['results']:
                    print(link[0])
    else:
        print("[-] 401 Unauthorized, make sure email and apikey is correct")